Fix contours() auto-levels silently dropping all contours with +/-inf#2801
Merged
Conversation
brendancol
commented
Jun 1, 2026
Contributor
Author
brendancol
left a comment
There was a problem hiding this comment.
PR Review: Fix contours() auto-levels silently dropping all contours with +/-inf
Blockers (must fix before merge)
None.
Suggestions (should fix, not blocking)
None.
Nits (optional improvements)
- xrspatial/contour.py:649 -- the numpy branch builds a full-size copy with
np.where(np.isfinite(agg.values), agg.values, np.nan). Since the data is already in memory, the cost is one transient array of the same size that the oldnp.nanmin(agg.values)did not allocate. Fine to leave as-is: it keeps the three branches reading the same way, and the extra peak memory only matters for very large numpy rasters. Flagging so it is a conscious call rather than an oversight. - xrspatial/tests/test_contour.py -- the file already has a
TestInfHandlingclass from #2704 that exercises the kernel/explicit-level path. The new tests live inTestAutoLevelsInfto dodge the name clash, which is the right move. The two classes hit different code (auto-level range vs. per-quad interpolation), so the similar names are not a problem.
What looks good
- The fix is in the shared pre-dispatch path, so one change covers numpy, cupy, dask+numpy, and dask+cupy identically.
- The dask branch stays lazy.
da.whereplusda.nanmin/da.nanmaxunder onedask.compute, same two-reduction shape as before, nothing newly materialized. - Swapping the guard from
np.isnantonot np.isfinitekeeps the old all-NaN behavior (returns[]) and now covers all-inf too. - The "All-NaN slice" RuntimeWarning from the all-non-finite case is suppressed in a tight scope around just that block, not globally.
- Tests cover the reported bug, a check that levels match the finite-only range, the all-inf empty case, explicit levels staying unaffected, and all four backends (cupy variants skip without a GPU).
Checklist
- NaN and inf handling is correct
- All implemented backends produce consistent results (numpy + dask verified locally; cupy paths skip without a GPU)
- Edge cases covered by tests (mixed inf, all-inf, explicit levels)
- Dask path stays lazy, no premature materialization
- Docstrings unchanged (no API change); contours already in reference docs
- No README matrix change needed (no new function, no backend change)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #2797. When
contours()generates levels automatically (levels=None), a+infor-infcell mixed with finite terrain madenanmin/nanmaxreturn infinity. Thenp.isnan-only guard let it through, andnp.linspace(vmin, inf, ...)then produced non-finite levels, so the function returned no contours at all. Every contour for the finite terrain disappeared, with no error.+/-inflike NaN) in the numpy, cupy, and dask branches.np.isnantonot np.isfiniteso an all-non-finite raster still returns an empty result cleanly.Backend coverage
numpy, cupy, dask+numpy, dask+cupy. The fix sits in the shared auto-level path before backend dispatch, so all four behave the same.
Test plan
TestAutoLevelsInfclass: auto-levels ignore+/-inf, levels match the finite-only range, all-inf returns empty, explicit levels unaffected.test_contour.pysuite passes (46 passed locally).